﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
// add
using System.Diagnostics;
using System.IO;
//using System.Windows.Forms;


namespace WebLib
{
    public class CAction
    {
        string msSubject;

        StringBuilder msLog = new StringBuilder();
        DateTime dBegin = new DateTime();
        DateTime dEnd = new DateTime();
        bool mbError;
        string msMessage;
        public int miStep { get; set; }
        public string Message { get { return msMessage; } }
        public string Subject { get { return msSubject; } }
        public bool IsError { get { return mbError; } }
        public string msResult { get; set; }
        public bool IsDebug { get; set; } // 預設開啟, 預設讀取設定後關閉.
        public string MessageLog { get { return msLog.ToString(); }}
        public DateTime BeginTime { get { return dBegin; } }
        public DateTime EndTime { get { return dEnd; } }
        public string msFilename { get; set; }
        private CAction() {}
        public CAction(string sSubject)
        {
            msSubject = sSubject;
            dBegin = DateTime.Now;
            IsDebug = true;

            mbError = false;
            msResult = string.Empty;
            miStep = 0;
            dBegin = DateTime.Now;
            dEnd = dBegin;

            ClearMsg();
            msLog.AppendLine(string.Format("{0},SUBJECT={1},{2}.", PrefixEndTime(), sSubject, dEnd.ToString("yyyy-MM-dd HH:mm:ss.fff")));
        }
        public void ClearMsg()
        {
            msMessage = string.Empty;
            dEnd = DateTime.Now;
            msLog.Length = 0;
        }
        public void AppendMsg(string sMsg)
        {
            msMessage = sMsg;
            dEnd = DateTime.Now;
            msLog.AppendLine(string.Format("{0},MSG={1}.", PrefixEndTime(), sMsg));
        }
        public void AppendMsgError(string sMsg)
        {
            msMessage = sMsg;
            dEnd = DateTime.Now;
            msLog.AppendLine(string.Format("{0},ERROR={1}.", PrefixEndTime(), sMsg));
        }
        public void AppendMsgDebug(string sMsg)
        {
            if (IsDebug)
            {
                msMessage = sMsg;
                dEnd = DateTime.Now;
                msLog.AppendLine(string.Format("{0},DEBUG={1}.", PrefixEndTime(), sMsg));
            }
        }
        public void Error(string sMsg)
        {
            Debug.WriteLine(sMsg);
            mbError = true;
            AppendMsgError(sMsg);
        }
        public void Success(string sResult)
        {
            Debug.WriteLine(sResult);
            mbError = false;
            msResult = sResult;
            dEnd = DateTime.Now;
            msLog.AppendLine(string.Format("{0},SUCCESS={1}.", PrefixEndTime(), sResult));
        }
        private string PrefixEndTime()
        {
            return dEnd.ToString("HH:mm:ss");
        }
        public void Begin(string sEventName)
        {
            Debug.WriteLine(msSubject);

            mbError = false;
            msResult = string.Empty;
            miStep = 0;
            dBegin = DateTime.Now;
            dEnd = dBegin;

            ClearMsg();
            msLog.AppendLine(string.Format("{0},BEGIN={1},{2}.", PrefixEndTime(), sEventName, dEnd.ToString("yyyy-MM-dd HH:mm:ss.fff")));
        }
        public void SaveLog()
        {
            dEnd = DateTime.Now;
            if (string.IsNullOrEmpty(msFilename))
                msFilename = @".\Log-" + dEnd.Second.ToString() + ".txt";

            File.WriteAllText(msFilename, MessageLog);
        }
        public void OpenWithNotePad()
        { 
        }
    }
}
